<template>
  <el-row>
    <el-col :span="5" style="border-right: 1px solid #dfe6ec">
      <customTree :ancestralId="listQuery.ancestralId" :btnTitle="' 创建模型规范根目录 '" @getId="getId" :type="true"
        @getTree="getTree" :expandType="'1'" />

    </el-col>
    <el-col :span="19">
      <div class="app-container">
        <div class="filter-container">
          <div class="filter-item">
            <span class="span">信息项名称:</span>
            <el-input v-model="listQuery.name" placeholder="请输入信息项名称" style="width: 200px" />
          </div>
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            {{ $t('table.search') }}
          </el-button>
          <el-button v-waves v-permission="['standardsystem:ModelSpecification:add']" plain class="filter-item"
            style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
            {{ $t('table.add') }}
          </el-button>
          <el-button v-waves class="filter-item" style="margin-left: 10px" plain type="success" icon="el-icon-download"
            @click="handleDown"> 下载信息项 </el-button>
          <el-button v-waves v-permission="['standardsystem:ModelSpecification:delete']" plain class="filter-item"
            style="margin-left: 10px" type="danger" icon="el-icon-delete" @click="handleBathDelete">
            {{ $t('table.delete') }}
          </el-button>

          <el-button v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest')
            }}</el-button>

          <!-- <el-button class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-upload" @click="handleImport">
            {{ $t('table.import') }}
          </el-button> -->
        </div>

        <el-table ref="table" :key="tableKey" v-loading="listLoading" element-loading-text="正在加载，请稍后" :data="list"
          border fit highlight-current-row @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
          <el-table-column label="信息项名称" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="英文名称" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.engName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="简称" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.shortName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数据类型" align="center" min-width="120px">
            <template slot-scope="{ row }">
              <span>{{ row.dataType | dictLabel('model_data_type') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="长度" align="center" min-width="120px">
            <template slot-scope="{ row }">
              <span>{{ row.dataSize }}</span>
            </template>
          </el-table-column>
          <el-table-column label="同义词" align="center" min-width="120px">
            <template slot-scope="{ row }">
              <span>{{ row.synonym }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" min-width="120px">
            <template slot-scope="{ row }">
              <el-switch class="tableScopeSwitch" v-model="row.status" :active-value="'1'" :inactive-value="'0'"
                active-text="启用" :inline-prompt="true" inactive-text="禁止" @change="changeType(row)" />
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" align="center" min-width="200" class-name="small-padding ">
            <template slot-scope="{ row }">
              <el-button v-permission="['standardsystem:ModelSpecification:detail']" type="text" size="small"
                icon="el-icon-tickets" @click="handledetail(row)">
                <!-- {{ $t('table.detail') }} -->
                详情
              </el-button>
              <el-button v-permission="['standardsystem:ModelSpecification:update']" type="text" size="small"
                icon="el-icon-edit" @click="handleUpdate(row)">
                {{ $t('table.edit') }}
              </el-button>
              <el-button v-permission="['standardsystem:ModelSpecification:delete']" type="text" size="small"
                icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
                {{ $t('table.delete') }}
              </el-button>
            </template>
          </el-table-column></el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
          :page-sizes="pageArray" @pagination="getList" />

        <model-specification-form ref="form" @refreshList="getList" />
        <importview ref="showimport" />
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { deleteModelSpecification, fetchModelSpecificationList, fileDown } from '@/api/standardsystem/modelSpecification'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import modelSpecificationForm from './modelSpecificationForm'
import Pagination from '@/components/Pagination'
// 上传文件组件
import importview from '@/components/modelImport/index.vue'

import customTree from '@/components/customTree/index.vue'
export default {
  name: 'ModelSpecificationList',
  components: { modelSpecificationForm, Pagination, importview, customTree },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      treeList: [],
      list: null,
      total: 0,
      multipleSelection: [],
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        name: undefined,
        ancestralId: '2',
        page: 1,
        limit: this.$store.getters.defaultPageSize,
        directoryId: '',
      },
      activeName: 'first', //tarbar
      NODE_KEY: 'id', // id对应字段
      MAX_LEVEL: 5, // 设定最大层级
      NODE_ID_START: 0, // 新增节点id，逐次递减
      startId: null,

    }
  },
  computed: {

    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },

  methods: {
    // 树形列表
    getId(data) {
      this.listQuery.ancestralId = '2'
      this.listQuery.directoryId = data.id
      this.getList()
    },
    getTree(data) {
      this.treeList = data
    },


    handleImport() {
      this.$refs.showimport.show()
    },
    getList() {
      this.listLoading = true
      fetchModelSpecificationList(this.listQuery).then((response) => {
        if (response.data.code == 200) {
          this.list = response.data.data.records
          this.total = response.data.data.total
          this.$nextTick(() => {
            this.$refs.table.doLayout()
          })
          this.listLoading = false
        } else {
          this.$message.error(response.data.msg)
          this.listLoading = false
        }
      })
    },   // 创建
    handleCreate() {
      this.$refs.form.setList(this.treeList)
      this.$refs.form.handleCreate()
    },  // 详情
    handleUpdate(row) {
      this.$refs.form.setList(this.treeList)
      this.$refs.form.handleUpdate(row.id)
    },    // 编辑
    handledetail(row) {
      this.$refs.form.setList(this.treeList)
      this.$refs.form.handledetail(row.id)
    },
    // 表格删除
    handleDelete(row) {
      let list = []
      list.push(row.id)
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        const params = { idList: list }
        deleteModelSpecification(params).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    // 下载
    handleDown() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      // fileDown(arr).then(res => {
      //
      // })

      this.$confirm('确定下载所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        fileDown({ ids: this.ids, responseType: 'arraybuffer' }).then((response) => {
          console.log(response)
          if (response.status == 200) {
            // import('@/vendor/Export2Excel').then(excel => {
            //   excel.export_byte_to_excel(response.data.data, '11')
            //   this.downloadLoading = false
            // })
            const objectUrl = URL.createObjectURL(
              new Blob([response.data], {
                type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
              })
            )
            const link = document.createElement('a')
            // 设置导出的文件名称
            link.download = `模型规范` + '.xlsx'
            link.style.display = 'none'
            link.href = objectUrl
            link.click()
            document.body.appendChild(link)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    // 表格多选
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    //重置
    handleRest() {
      this.listQuery = {
        directoryId: this.listQuery.directoryId,
        page: this.listQuery.page,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },
    // 删除
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let list = this.ids.split(',')
        // ===================批量删除
        const params = { idList: list }
        deleteModelSpecification(params).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}

.tableScopeSwitch .el-switch__label {
  position: absolute;
  display: none;
  color: #fff;
}

/*打开时文字位置设置*/
.tableScopeSwitch .el-switch__label--right {
  z-index: 1;
  right: 6px;
}

/*关闭时文字位置设置*/
.tableScopeSwitch .el-switch__label--left {
  z-index: 1;
  left: 6px;
}

/*显示文字*/
.tableScopeSwitch .el-switch__label.is-active {
  display: block;
}

.tableScopeSwitch.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 60px !important;
  /*开关按钮的宽度大小*/
}

.el-tree-node__content {
  height: 32px !important;
}

.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}

.el-row {
  display: flex;
}

.el-col-5 {
  flex: 1;
}
</style>
